package com.gomore.opple.module.account.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.gomore.opple.R;
import com.gomore.opple.web.cgform.goods.view.VOResellerGoodsEntity;
import com.gomore.opple.web.cgform.shopcart.entity.TOShopcartEntity;

import java.math.BigDecimal;
import java.util.List;

/**
 * Created by asus on 2017/5/17.
 */
public class AccountAdapter extends BaseAdapter {
    Context mContext;
    List<TOShopcartEntity> mtoShopcartEntityList;

    public AccountAdapter(Context context, List<TOShopcartEntity> toShopcartEntityList) {
        mContext = context;
        mtoShopcartEntityList = toShopcartEntityList;
    }

    @Override
    public int getCount() {
        return mtoShopcartEntityList.size();
    }

    @Override
    public Object getItem(int position) {
        return mtoShopcartEntityList == null ? 0 : mtoShopcartEntityList.size();
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder;
        if (convertView == null) {
            viewHolder = new ViewHolder();
            convertView = LayoutInflater.from(parent.getContext()).inflate(R.layout.account_item, parent, false);
            viewHolder.image_icon = (ImageView) convertView.findViewById(R.id.image_icon);
            viewHolder.name = (TextView) convertView.findViewById(R.id.name);
            viewHolder.code = (TextView) convertView.findViewById(R.id.code);
            viewHolder.done_price = (TextView) convertView.findViewById(R.id.done_price);
            viewHolder.good_number = (TextView) convertView.findViewById(R.id.good_number);
            viewHolder.total = (TextView) convertView.findViewById(R.id.total);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }
        TOShopcartEntity toShopcartEntity = mtoShopcartEntityList.get(position);
        if (toShopcartEntity != null) {
            VOResellerGoodsEntity voResellerGoodsEntity = toShopcartEntity.getGoods();
            if (voResellerGoodsEntity != null) {
                String url = "demo";
                if (voResellerGoodsEntity.getPictures() != null) {
                    String[] urls = voResellerGoodsEntity.getPictures().split(",");
                    url = urls[0];
                }
                Glide.with(mContext)
                        .load(url)
                        .asBitmap()
                        .placeholder(R.mipmap.goods_place)
                        .into(viewHolder.image_icon);
                if (voResellerGoodsEntity.getName() != null) {
                    viewHolder.name.setText(voResellerGoodsEntity.getName());
                }
                if (voResellerGoodsEntity.getCode() != null) {
                    viewHolder.code.setText(voResellerGoodsEntity.getCode());
                }
                if (toShopcartEntity.getCostPrice() != null) {
                    viewHolder.done_price.setText("¥ " + toShopcartEntity.getCostPrice().toString());
                }
                if (toShopcartEntity.getGoodsNum() != null) {
                    viewHolder.good_number.setText("" + toShopcartEntity.getGoodsNum());
                }
                if (toShopcartEntity.getCostPrice() != null && toShopcartEntity.getGoodsNum() != null) {
                    viewHolder.total.setText("¥" + toShopcartEntity.getCostPrice().multiply(new
                            BigDecimal(toShopcartEntity.getGoodsNum())).toString());
                }
            }
        }
        return convertView;
    }

    class ViewHolder {
        ImageView image_icon;
        TextView name, code, done_price, good_number, total;
    }
}
